namespace Warehouse

open System.Windows
open System.Windows.Data
open System.Windows.Controls
open FSharp.Windows
open Warehouse.Controls

type InvoiceDetailsEvents = 
    | Save of (unit -> unit)
    | BulkInput 
    | AddLineItem 
    | FromMonsoon

type InvoiceDetailsView() as this = 
    inherit View<InvoiceDetailsEvents, InvoiceDetailsModel, InvoiceDetailsWindow>()

    let client = this.Control.Client

    do
        client.DisplayMemberPath <- "CompanyName"
        this.Control.Note.MaxLength <- 100
        this.CancelButton <- this.Control.Cancel
        this.Control.PctOverride.EnsureDecimal()

    override this.EventStreams = 
        [
            this.Control.Save, Save this.OK
            this.Control.BulkInput, BulkInput
            this.Control.Add, AddLineItem
            this.Control.FromMonsoon, FromMonsoon
        ] 
        |> List.ofButtonClicks

    override this.SetBindings model = 

        Binding.FromExpression 
            <@
                client.ItemsSource <- model.AllClientsList
                client.SelectedItem <- model.Client

                this.Control.Note.Text <- model.Note
                this.Control.PctOverride.Text <- string model.CommissionOverride
                this.Control.LineItems.ItemsSource <- model.LineItems

                this.Control.FromMonsoon.IsEnabled <- model.IsFromMonsoonEnable
            @>        

        Binding.TwoWay
            <@
                this.Control.Date.Text <- string model.Date
            @>

        this.Control.LineItems.SetBindings(
            model,
            itemsSource = <@ model.LineItems @>,
            columnBindings = fun item ->
                [
                    this.Control.UPC, <@@ item.UPCode @@>
                    this.Control.Quantity, <@@ item.Quantity @@>
                ]
        )

